//引入模块
var https = require('https')
var fs = require('fs');
var iconv = require('iconv-lite')
const users = require('./users')
const headers = require('./header');
const { default: header } = require('./header');
module.exports = function () {
  let proArr = []
  let index = 0;
  let result = ''


  users.map((v, i) => {
    let index = 0;
    let result = ''
    let pro = new Promise((resolve, reject) => {
      let name = v.name
      https.get("https://weibo.com/ajax/profile/info?custom=" + v.id, {
        "headers": header,
        "body": null,
        "method": "GET"
      }, function (res) {
        let chunks = []
        res.on("data", function (chunk) {
          chunks.push(chunk);
        })
        res.on('end', () => {
          //将获取的代码整合在一起
          var str = Buffer.concat(chunks);
          //处理乱码
          var data = iconv.decode(str, 'utf-8');
          data = JSON.parse(data)
          index++
          let word = name + ':' + data.data.user.followers_count + '；'
          result += word
          if (index == users.length) {
            console.log(result)
            response.setHeader("Content-Type", 'text/html; charset=utf-8')
            response.end(result);
          }
        })
      })
    })
    proArr.push(pro)
  })
  Promise.all(proArr).then(res=>{
    console.log(res)
  })
  return proArr
}